package net.mihome.app.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import net.mihome.app.AppContext;
import net.mihome.app.R;
import net.mihome.app.bean.RssResource;
import net.mihome.app.ui.RssNewsListActivity;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

/**
 * 用户博客Adapter类
 * @author liux (http://my.oschina.net/liux)
 * @version 1.0
 * @created 2012-3-21
 */
public class RssTypeAdapter extends BaseAdapter {
	private Context 					context;//运行上下文
	private List<RssResource> 					list;//数据集合
	private AppContext appContext;
	private LayoutInflater 				listContainer;//视图容器
	SimpleDateFormat sdFormat=new SimpleDateFormat("MM月dd日hh点mm分ss秒");

	/**
	 * 实例化Adapter
	 * @param context
	 * @param data
	 * @param resource
	 */
	public RssTypeAdapter(Context context, List<RssResource> rssResourceList) {
		this.context = context;			
		this.listContainer = LayoutInflater.from(context);	//创建视图容器并设置上下文
		this.list=rssResourceList;
		this.appContext=(AppContext) context.getApplicationContext();
	}
	
	public int getCount() {
		return list.size();
	}

	public Object getItem(int arg0) {
		return list.get(arg0);
	}

	public long getItemId(int arg0) {
		return list.get(arg0).getId();
	}
	
	/**
     * 下面是重要代码
     */
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
	        LayoutInflater _LayoutInflater=LayoutInflater.from(this.context);
	        convertView=_LayoutInflater.inflate(R.layout.rss_type_lv_item, null);
	        if(convertView!=null)
	        {
	            TextView item_tTextView=(TextView)convertView.findViewById(R.id.rss_type_item_tv_id);
	            item_tTextView.setText(list.get(position).getTitle());
	            
	            TextView last_TextView=(TextView) convertView.findViewById(R.id.rss_type_item_tv_last);
	            String last_time=appContext.getProperty("rss_last"+list.get(position).getTitle());
	            if(last_time!=null){
		            last_TextView.setText("上次更新:"+last_time);
	            }else{
	            	last_TextView.setText("");
	            }
	        }
	        setOnclick(convertView);
        return convertView;
    }
    /**
     * 添加点击事件
     * */
    public void setOnclick(View view){
    	view.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View view) {
				TextView textView=(TextView) view.findViewById(R.id.rss_type_item_tv_id);
				String typeTitle=textView.getText().toString();
				appContext.setProperty("rss_last"+typeTitle,sdFormat.format(new Date()));
				Iterator<RssResource> iterator=list.iterator();
				while(iterator.hasNext()){
					RssResource rssResource=iterator.next();
					if(rssResource.getTitle().equals(typeTitle)){
						Intent intent=new Intent(context,RssNewsListActivity.class);
						intent.putExtra("rssResource", rssResource.getHref());
						context.startActivity(intent);
					}
				}
			}
		});
    }
}